//
//  PayDAO.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/12/25.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "PayDAO.h"
#import "DBHelper.h"

#define TABLE_NAME [UserManager userMd5IdentiferPath:@"pay"]

#define ID @"_id" // 本地数据库ID
#define ORDER_ID @"order_id"
#define LINE_ID @"line_id"
#define TRANS_ID @"transaction_id"
#define PAY_STATE @"pay_state"// 订单状态

#define PAY_INFO @"pay_info"// 支付结果

#define CREATE_TIME @"create_time"// 下单时间
#define UPDATE_TIME @"update_time"// 更新时间
#define REMARK @"remark"// 备注信息
#define CURRENCY @"currency"

@implementation PayDAO

+ (void)insertPayModel:(PayModel *)model {
    
    FMDatabaseQueue *queue = [DBHelper getDatabaseQueue];
    
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
       
        NSString *createsql = [NSString stringWithFormat:@"create table if not exists %@(%@ integer not null primary key autoincrement,%@ text not null unique, %@ text,%@ text, %@ integer, %@ text, %@ date,%@ date,%@ text,%@ text)",TABLE_NAME,ID,ORDER_ID,LINE_ID,TRANS_ID,PAY_STATE,PAY_INFO,CREATE_TIME,UPDATE_TIME,REMARK,CURRENCY];
        
        [db executeUpdate:createsql];
        
        NSString *insertsql = [NSString stringWithFormat:@"insert or replace into %@ (%@,%@,%@,%@,%@,%@,%@,%@,%@) values (?,?,?,?,?,?,?,?,?)",TABLE_NAME,ORDER_ID,LINE_ID,TRANS_ID,PAY_STATE,PAY_INFO,CREATE_TIME,UPDATE_TIME,REMARK,CURRENCY];
        
        [db executeUpdate:insertsql,model.orderid,model.lineid,model.transid,@(model.payState),model.payinfo,model.createtime,[NSDate date],model.remark,model.currency];
    }];
}

+ (void)deletePayModelWithOrderid:(NSString *)orderid {
    
    FMDatabaseQueue *queue = [DBHelper getDatabaseQueue];
    
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        
        NSString *deletesql = [NSString stringWithFormat:@"delete from %@ where %@ = ?",TABLE_NAME,ORDER_ID];
        
        BOOL success = [db executeUpdate:deletesql,orderid];
        
        NSLog(@"delete %@ %d",orderid,success);
    }];
}

+ (void)updatePayStateWithOrderid:(NSString *)orderid state:(PayState)state {
    
    FMDatabaseQueue *queue = [DBHelper getDatabaseQueue];
    
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        
        NSString *updatesql = [NSString stringWithFormat:@"update %@ set %@ = ? where %@ = ?",TABLE_NAME,PAY_STATE,ORDER_ID];
        
        [db executeUpdate:updatesql,@(state),orderid];
    }];
}

+ (PayModel *)getPayModelWithTransid:(NSString *)transid {
    
    FMDatabaseQueue *queue = [DBHelper getDatabaseQueue];
    
    __block PayModel *model = nil;
    
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        
        NSString *querysql = [NSString stringWithFormat:@"select * from %@ where %@ = ?",TABLE_NAME,TRANS_ID];
        
        FMResultSet *set = [db executeQuery:querysql,transid];
        
        if ([set next]) {
            
            model = [self getPayModelWithRS:set];
            
        }
    }];
    
    return model;
}

+ (PayModel *)getPayModelWithOrderid:(NSString *)orderid {
    
    FMDatabaseQueue *queue = [DBHelper getDatabaseQueue];
    
    __block PayModel *model = nil;
    
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        
        NSString *querysql = [NSString stringWithFormat:@"select * from %@ where %@ = ?",TABLE_NAME,ORDER_ID];
        
        FMResultSet *set = [db executeQuery:querysql,orderid];
        
        if ([set next]) {
            
            model = [self getPayModelWithRS:set];
            
        }
    }];
    
    return model;
}

+ (NSArray <PayModel *>*)getAllOrderWithState:(PayState)state {
    
    FMDatabaseQueue *queue = [DBHelper getDatabaseQueue];
    
    NSMutableArray *paymodelArray = [NSMutableArray array];
    
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        
        NSString *querysql = [NSString stringWithFormat:@"select * from %@ where %@ = ? order by %@ desc",TABLE_NAME,PAY_STATE,UPDATE_TIME];
        
        FMResultSet *set = [db executeQuery:querysql,@(state)];
        
        while ([set next]) {
            
            PayModel *model = [self getPayModelWithRS:set];
            if (model) {
                [paymodelArray addObject:model];
            }
        }
    }];
    
    return paymodelArray;
}

+ (PayModel *)getPayModelWithRS:(FMResultSet *)set {
    
    PayModel *model = nil;
    
    NSString *orderid = [set stringForColumn:ORDER_ID];
    
    if (!orderid) {
        return model;
    }
    
    model = [PayModel new];
    model.orderid = orderid;
    model.lineid = [set stringForColumn:LINE_ID];
    model.transid = [set stringForColumn:TRANS_ID];
    model.payinfo = [set stringForColumn:PAY_INFO];
    model.remark = [set stringForColumn:REMARK];
    model.currency = [set stringForColumn:CURRENCY];
    
    model.createtime = [set dateForColumn:CREATE_TIME];
    model.updatetime = [set dateForColumn:UPDATE_TIME];
    model.payState = [set intForColumn:PAY_STATE];
    
    return model;
}

@end
